import { photoMaxSize, scaleData } from "../config";
import { IGlobalConfig, IPhotoInfo, ISceneConfig } from "../type";

// 计算比例宽高
const computeScaleHW = (photoInfo: IPhotoInfo) => {
  const { scale, direction } = photoInfo;
  let { width, height } = scaleData[scale] ?? {};
  if (!width || !height) {
    height = photoMaxSize;
    width =
      (direction === "vertical"
        ? photoInfo.width / photoInfo.height
        : photoInfo.height / photoInfo.width) * height;
  }
  return {
    sceneH: direction === "vertical" ? height : width,
    sceneW: direction === "vertical" ? width : height,
  };
};

// 计算场景数据
const computeSceneConfig = (
  photoInfo: IPhotoInfo,
  globalConfig: IGlobalConfig
): ISceneConfig => {
  const { sceneW, sceneH } = computeScaleHW(photoInfo);

  let totalW, totalH, pixiImgHeight;
  if (photoInfo.isSign) {
    totalW = sceneW - photoInfo.padding * 2; //左右都添加边框
    totalH = sceneH - photoInfo.padding; // 只有顶部需要添加边框
    pixiImgHeight = totalH - globalConfig.infoHeight; // 图片高度
  } else {
    totalW = sceneW - photoInfo.padding * 2; //左右都添加边框
    totalH = sceneH - photoInfo.padding * 2; // 上下都添加边框
    pixiImgHeight = totalH; // 图片高度
  }

  return {
    infoHeight: globalConfig.infoHeight,
    infoPadding: globalConfig.infoPadding,
    totalW: sceneW,
    totalH: sceneH,
    width: totalW,
    height: totalH,
    imgHeight: pixiImgHeight,
  };
};

export { computeScaleHW, computeSceneConfig };
